package seekting.demo2017.activity;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;

import com.seekting.demo_lib.Demo;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.Socket;

import seekting.demo2017.R;

/**
 * Created by seekting on 17-6-20.
 */

@Demo(title = "http协议的理解", desc = "用socket模拟对http协议的请求")
public class HttpActivity extends Activity implements View.OnClickListener {
    private View socket_request_get, socket_request_post;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.http_layout);
        socket_request_get = findViewById(R.id.socket_request_get);
        socket_request_post = findViewById(R.id.socket_request_post);
        socket_request_get.setOnClickListener(this);
        socket_request_post.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.socket_request_get:
            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {

                        socket_request_get();
                    } catch (Throwable throwable) {
                        throwable.printStackTrace();
                    }
                }
            }).start();


            break;
        case R.id.socket_request_post: {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {

                        socket_request_post();
                    } catch (Throwable throwable) {
                        throwable.printStackTrace();
                    }
                }
            }).start();
            break;
        }
        }
    }

    private void socket_request_post() throws Throwable {
        int port = 80;
        String host = "www.wrox.com";
        String path = "/";
        String charset = "utf-8";
        String method = "POST";
        Socket s = new Socket(host, port);
        BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream(), charset));
        OutputStream out = s.getOutputStream();
        StringBuffer sb = new StringBuffer(method + " " + path + " HTTP/1.1\r\n");
        sb.append("User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022)\r\n");
        sb.append("Host:" + host + "\r\n");
//        sb.append("Accept: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2\r\n");
        sb.append("Connection: Keep-Alive\r\n");
        sb.append("charset: " + charset + "\r\n");
        sb.append("\r\n");
        sb.append("publisher=Wiley");
        out.write(sb.toString().getBytes(charset));
        String tmp = "";
        while ((tmp = br.readLine()) != null) {
            System.out.println(tmp);
        }
        out.close();
        br.close();

    }

    private void socket_request_get() throws Throwable {
        int port = 80;
        String host = "www.baidu.com";
        String path = "/s?wd=lofter";
        String charset = "utf-8";
        String method = "GET";
        Socket s = new Socket(host, port);
        BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream(), charset));
        OutputStream out = s.getOutputStream();
        StringBuffer sb = new StringBuffer(method + " " + path + " HTTP/1.1\r\n");
        sb.append("User-Agent: Java/1.6.0_20\r\n");
        sb.append("Host:" + host + ":" + port + "\r\n");
        sb.append("Accept: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2\r\n");
        sb.append("Connection: Close\r\n");
        sb.append("charset: " + charset + "\r\n");
        sb.append("\r\n");
        out.write(sb.toString().getBytes(charset));
        String tmp = "";
        while ((tmp = br.readLine()) != null) {
            System.out.println(tmp);
        }
        out.close();
        br.close();

    }


}
